﻿using GenericityDispatch.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDispatch.DispatchRealization
{
    /// <summary>
    ///  实现基类
    /// </summary>
    /// <typeparam name="TEvent"></typeparam>
    public abstract class BaseRealization<TEvent> : IEventHandler<TEvent>
        where TEvent : Event
    {
        public Task HandleAsync(TEvent @event)
        {
            try
            {
                return Task.Run(() => Receive(@event));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 调度处理中心
        /// </summary>
        /// <param name="eEvent"></param>
        public void Handle(TEvent @Event)
        {
            try
            {
                Receive(@Event);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 节点处理消息
        /// </summary>
        /// <param name="eEvent">消息体</param>
        protected abstract void Receive(TEvent eEvent);
    }
}
